<template>
	<view class="l_pages">
		<jnavbar :title="'核销'"></jnavbar>
		<scroll-view class="page_container">
			<block v-if="veriflist.length > 0">
				<view class="verif_div">
					<text class="title">待核销</text>
					<view class="list uni-flex-column">
						<view class="item uni-flex-row uni-flex-title" v-for="(item, i) in veriflist" @click="item.checked = !item.checked" :key="i">
							<view class="uni-flex-row">
								<vimage class="img_box" :src="item.master" mode=""></vimage>
								<view class="uni-flex-column info_text">
									<text class="u-line-2">{{ item.commodity_name }}</text>
									<text class="sm-font">{{ item.pvs_value }}</text>
									<text>x{{ item.count }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<block v-if="diffList.length > 0">
				<view class="verif_div">
					<text class="title">已核销</text>
					<view class="list uni-flex-column">
						<view class="item uni-flex-row uni-flex-title" v-for="(item, i) in diffList" @click="item.checked = !item.checked" :key="i">
							<view class="uni-flex-row">
								<vimage class="img_box" :src="item.master" mode=""></vimage>
								<view class="uni-flex-column info_text">
									<text class="u-line-2">{{ item.commodity_name }}</text>
									<text class="sm-font">{{ item.pvs_value }}</text>
									<text>x{{ item.count }}</text>
								</view>
							</view>
							<text>{{ item.status_text }}</text>
						</view>
					</view>
				</view>
			</block>
			<view class="btns">
				<u-button :custom-style="customStyle" @click="saoma">核销扫码</u-button>
				<block v-if="veriflist.length > 0">
					<u-button :custom-style="customStyle1" v-if="accepttype == 2" @click="accept">确认核销</u-button>
					<u-button :custom-style="customStyle1" v-if="accepttype == 3" @click="acceptin('/integral/shop/accept/confirm')">确认核销</u-button>
					<u-button :custom-style="customStyle1" v-if="accepttype == 1" @click="acceptin('/lottery/accept/confirm')">确认核销</u-button>
				</block>
				<u-button :custom-style="customStyle1" v-else @click="tolist">核销记录</u-button>
			</view>
			<u-popup v-model="show" mode="center" border-radius="20">
				<view class="p_div">
					<vimage class="image" imgtype="1" src="static/images/accept_success.png" mode="widthFix"></vimage>
					<text>订单核销成功</text>
					<view class="view" @click="show = false">知道了</view>
				</view>
			</u-popup>
		</scroll-view>
		<tab-bar ref="tabbar"></tab-bar>
		<uni-login ref="Login" @callback="logincallback"></uni-login>
	</view>
</template>

<script>
import util from '@/common/util.js';
import http from '@/common/http.js';
import {scanQRCode} from '@/common/wxJsSDK';
import {mapState, mapMutations} from 'vuex';
export default {
	data() {
		return {
			veriflist: [],
			diffList: [],
			detail: {},
			show: false,
			customStyle: {
				width: '318rpx',
				height: '90rpx',
				border: '2rpx solid #ff4206',
				'border-radius': '10rpx',
				'font-size': '34rpx',
				'font-weight': 'bold',
				background: '#f4f4f4',
				color: '#ff4206'
			},
			customStyle1: {
				width: '318rpx',
				height: '90rpx',
				'border-radius': '10rpx',
				'font-size': '34rpx',
				'font-weight': 'bold',
				background: 'linear-gradient(90deg, #fe8e0a, #ff5011)',
				color: '#ffffff',
				border: 'none'
			},
			accepttype: 2
		};
	},
	onShareAppMessage() {
		return this.wxShare();
	},
	async onLoad(e) {
		await this.$getWx;
		var _this = this;
		this.$nextTick(a => {
			_this.$refs.tabbar.gettabs();
		});
		if (e.a == '1') {
			this.accepttype = 1;
			this.lotterylist(e.ids);
			this.ids = e.ids;
		} else if (e.a == '2') {
			this.accepttype = 3;
			this.store_id = e.stoer_id;
			this.integrallist(e.ids);
			this.ids = e.ids;
		}
		if (e.order && e.ids && e.store && e.user) {
			this.accepttype = 2;
			let order = e.order;
			let ids = e.ids;
			let store = e.store;
			let user = e.user;
			let data = {
				order: order,
				ids: ids,
				store: store,
				user: user
			};
			this.detail = data;
			this.judgeLogin(
				() => {
					_this.$store.commit('setlogin', false);
				},
				() => {
					_this.getlist(data);
				}
			);
		} else if (e.scene) {
			this.getwxaQr(e.scene);
		}
	},
	computed: {
		...mapState({
			hastabbar: state => state.hastabbar
		})
	},
	methods: {
		//积分商品核销
		integrallist(id) {
			let data = {
				ids: id
			};
			http('/integral/shop/accept/list', data, 'POST').then(res => {
				if (res.statusCode == 200) {
					let data = res.data.msg[0].orderCommodity;

					let item = {
						master: data.master,
						commodity_name: data.name,
						pvs_value: data.pvs_value,
						count: data.count
					};
					if (res.data.msg[0].status == 2) {
						this.veriflist = [];
						this.veriflist.push(item);
					} else if (res.data.msg[0].status == 3) {
						this.diffList = [];
						this.diffList.push(item);
					}
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		},
		lotterylist(ids) {
			let data = {
				ids: ids
			};
			http('/lottery/accept/list', data, 'POST').then(res => {
				if (res.statusCode == 200) {
					let data = JSON.parse(res.data.msg[0].prize.commodity);
					let item = {
						master: data.master,
						commodity_name: data.name,
						pvs_value: data.pvs_value,
						count: res.data.msg[0].number
					};
					this.store_id = res.data.msg[0].Logistics.store_id;
					if (res.data.msg[0].Logistics.status == 1) {
						this.veriflist = [];
						this.veriflist.push(item);
					} else if (res.data.msg[0].Logistics.status == 3) {
						this.diffList = [];
						this.diffList.push(item);
					}
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		},
		acceptin(url) {
			let _this = this;
			uni.showModal({
				title: '是否确认核销',
				success(res) {
					if (res.confirm) {
						let data = {
							ids: _this.ids
						};
						if (_this.store_id) {
							data.store = _this.store_id;
						}
						http(url, data, 'POST').then(res => {
							_this.show = true;
							_this.veriflist = [];
						});
					}
				}
			});
		},
		tolist() {
			this.Jumpurl({
				link: '../accept/list',
				type: 1,
				login_type: 2
			});
		},
		logincallback(e) {
			if (e.islogin) {
				this.getlist(this.detail);
			} else {
				this.Jumpurl({
					type: 1,
					link: '../index/index',
					login_type: 1
				});
			}
		},
		getwxaQr(wxa) {
			http('/order/accept/wxaQr?wxa=' + wxa, {}, 'GET').then(res => {
				let data = JSON.parse(res.data.msg);
				this.detail = data;
				this.getlist(data);
			});
		},
		analysis(result){
			if (!result) {
				uni.showToast({
					title: '无效二维码',
					icon: 'none'
				});
			}
			let a = util.getQueryVariable('a', result);
			let order = util.getQueryVariable('order', result);
			let ids = util.getQueryVariable('ids', result);
			let store = util.getQueryVariable('store', result);
			let user = util.getQueryVariable('user', result);
			let store_id = util.getQueryVariable('user', result);
			if (a == '2') {
				this.accepttyp = 1;
				this.ids = ids;
				this.store_id = store_id;
				this.integrallist(ids);
			}
			if (a == '1') {
				this.accepttyp = 1;
				this.ids = ids;
				this.lotterylist(ids);
			}
			if (order && ids && store && user) {
				ids = decodeURIComponent(ids);
				let data = {
					order: order,
					ids: ids,
					store: store,
					user: user
				};
				this.detail = data;
				this.getlist(data);
			} else {
				uni.showToast({
					title: '无效二维码',
					icon: 'none'
				});
			}
		},
		saoma() {
			var _this = this;
			// #ifdef H5
			scanQRCode(res => {
				_this.analysis(res.resultStr)
			}, 1);
			// #endif

			// #ifdef MP-WEIXIN

			uni.scanCode({
				onlyFromCamera: true,
				success: function(res) {
					console.log('条码内容：' + JSON.stringify(res));
					if (res.scanType == 'WX_CODE') {
						let result = res.path;
						if (!result) {
							uni.showToast({
								title: '无效二维码',
								icon: 'none'
							});
						}
						let wxa = util.getQueryVariable('scene', result);
						if (wxa) {
							_this.getwxaQr(wxa);
						} else {
							uni.showToast({
								title: '无效二维码',
								icon: 'none'
							});
						}
					}else{
						_this.analysis(res.result)
					}
				},
				fail(e) {
					uni.showToast({
						title: '无效二维码',
						icon: 'none'
					});
				}
			});
			// #endif
		},
		getlist(data) {
			http('/order/accept', data, 'GET').then(res => {
				if (res.statusCode == 200) {
					this.veriflist = res.data.msg.list;
					this.diffList = res.data.msg.diffList;
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		},
		accept() {
			let data = {
				order: this.detail.order,
				store: this.detail.store,
				user: this.detail.user
			};
			let ids = this.detail.ids.split(',');
			ids.forEach((a, i) => {
				data['ids[' + i + ']'] = a;
			});

			http('/order/accept', data, 'POST').then(res => {
				if (res.statusCode == 200) {
					// uni.showToast({
					// 	title: '核销成功',
					// 	icon: 'none'
					// });
					this.show = true;
					this.veriflist = [];
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		}
	}
};
</script>

<style scoped lang="scss">
.title {
	display: block;
	padding: 24rpx;
	font-size: 32rpx;
}
.verif_div {
	width: 713rpx;
	margin: 30rpx auto;
	background: #ffffff;
	border-radius: 20rpx;
	overflow: hidden;
}
.list {
	.item {
		font-size: 30rpx;
		color: #333;
		// border-bottom: 1rpx solid #f5f5f5;
		padding: 20rpx;
		// margin-bottom: 20rpx;
		background-color: #fff;
		.img_box {
			width: 180rpx;
			height: 180rpx;
			border-radius: 6rpx;
			margin-right: 26rpx;
		}
		.info_text {
			flex: 1;
			font-size: 28rpx;
			// justify-content: space-between;
			height: 180rpx;
			.u-line-2 {
				font-size: 26rpx;
				color: #333333;
				line-height: 46rpx;
				height: 92rpx;
			}
			// padding-left: 20rpx;
			.sm-font {
				font-size: 24rpx;
				color: #d1d1d1;
				margin: 10rpx 0;
			}
		}
		.iconfont {
			font-size: 40rpx;
		}
		.active {
			.iconfont {
				color: #ff3c29;
			}
		}
	}
}

.btns {
	display: flex;
	position: absolute;
	bottom: 60rpx;
	width: 100%;
	padding: 0 46rpx;
	justify-content: space-between;
	.btn {
		width: 318rpx;
		height: 90rpx;
		border: 2rpx solid #ff4206;
		border-radius: 10rpx;
		font-size: 34rpx;

		background: #f4f4f4;
		color: #ff4206;

		&.sure {
			background: linear-gradient(90deg, #fe8e0a, #ff5011);
			color: #ffffff;
			border: none;
		}
	}
}
.p_div {
	width: 551rpx;
	height: 535rpx;
	background: #ffffff;
	border-radius: 20rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	overflow: hidden;
	.image {
		width: 186rpx;
		height: 171rpx;
		margin: 100rpx 0 50rpx 0;
	}
	text {
		font-size: 40rpx;
		font-weight: bold;
		color: #333333;
	}
	.view {
		width: 551rpx;
		height: 88rpx;
		background: #fff5f2;
		border-radius: 0 0 20rpx 20rpx;
		text-align: center;
		line-height: 88rpx;
		position: absolute;
		bottom: 0;
		font-size: 34rpx;
		font-weight: bold;
		color: #ff6133;
	}
}
</style>
